package com.beiing.tianshuai.tianshuai.mine.model;

import com.beiing.tianshuai.tianshuai.entity.UpdatePersonalInfoBean;
import com.beiing.tianshuai.tianshuai.network.DataManager;
import com.beiing.tianshuai.tianshuai.util.LogUtils;

import java.io.File;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.observers.DisposableObserver;
import io.reactivex.schedulers.Schedulers;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;

/**
 * Created by Jinni on 2017/7/26 0026.
 */

public class PersonalInfoModel implements PersonalInfoModelImpl {

    private static final String TAG = "PersonalInfoModel";
    private OnRequestListener mListener;

    public PersonalInfoModel(OnRequestListener listener) {
        mListener = listener;
    }

    @Override
    public void getEditPersonalInfoResult(String uid, String name, String sex, String tel, String unit, String sign, String email) {
        //构建body
        RequestBody requestBody = new MultipartBody.Builder().setType(MultipartBody.FORM)
                .addFormDataPart("uid", uid)
                .addFormDataPart("name", name)
                .addFormDataPart("sex", sex)
                .addFormDataPart("email", email)
                .addFormDataPart("tel", tel)
                .addFormDataPart("unit", unit)
                .addFormDataPart("sign", sign)
                .build();

        Observable<UpdatePersonalInfoBean> loginResult = DataManager.getUpdatePersonalInfoResult(
                requestBody
        );
        Observable<UpdatePersonalInfoBean> signUpBeanObservable = loginResult.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread());
        signUpBeanObservable.subscribe(new DisposableObserver<UpdatePersonalInfoBean>() {

            @Override
            public void onNext(@NonNull UpdatePersonalInfoBean bean) {
                if (mListener != null) {
                    mListener.onSuccess(bean);
                }
            }

            @Override
            public void onError(@NonNull Throwable e) {
                if (mListener != null) {
                    mListener.onError(e);
                }
            }

            @Override
            public void onComplete() {
                LogUtils.i("------------------ onCompleted ------------------", TAG);
            }
        });
    }

    @Override
    public void getUpdateAvatarResult(String uid, File avatar) {
        //构建body
        RequestBody requestBody = new MultipartBody.Builder().setType(MultipartBody.FORM)
                .addFormDataPart("uid", uid)
                .addFormDataPart("picname", avatar.getName(), RequestBody.create(MediaType.parse("image/*"), avatar))
                .build();

        Observable<UpdatePersonalInfoBean> loginResult = DataManager.getUpdateAvatarResult(
                requestBody
        );
        Observable<UpdatePersonalInfoBean> signUpBeanObservable = loginResult.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread());
        signUpBeanObservable.subscribe(new DisposableObserver<UpdatePersonalInfoBean>() {

            @Override
            public void onNext(@NonNull UpdatePersonalInfoBean bean) {
                if (mListener != null) {
                    mListener.updateAvatarSuccess(bean);
                }
            }

            @Override
            public void onError(@NonNull Throwable e) {
                if (mListener != null) {
                    LogUtils.e(e.getMessage(), TAG);
                    mListener.onError(e);
                }
            }

            @Override
            public void onComplete() {
                LogUtils.i("------------------ onCompleted ------------------", TAG);
            }
        });
    }

    public interface OnRequestListener {

        void onSuccess(UpdatePersonalInfoBean bean);

        void updateAvatarSuccess(UpdatePersonalInfoBean bean);

        void onError(Throwable throwable);
    }
}
